package com.banciyuan.bcywebview.base.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;


public class ChooseDialog extends Dialog {
    public ChooseDialog(Context context) {
        super(context);
    }

    public ChooseDialog(Context context, int theme) {
        super(context, theme);
    }

    protected ChooseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context mContext;
        private TextView tvChooseOne, tvChooseTwo, tvChooseThree, tvChooseFour;
        private View viewChooseTwo, viewChooseThree, viewChooseFour;
        private String chooseOneString, chooseTwoString, chooseThreeString, chooseFourString;
        private View.OnClickListener chooseOneClick, chooseTwoClick, chooseThreeClick, chooseFourClick;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setChooseOneString(String chooseOneString) {
            this.chooseOneString = chooseOneString;
            return this;
        }

        public Builder setChooseTwoString(String chooseTwoString) {
            this.chooseTwoString = chooseTwoString;
            return this;
        }

        public Builder setChooseThreeString(String chooseThreeString) {
            this.chooseThreeString = chooseThreeString;
            return this;
        }

        public Builder setChooseFourString(String chooseFourString) {
            this.chooseFourString = chooseFourString;
            return this;
        }

        public Builder setChooseOneClick(View.OnClickListener chooseOneClick) {
            this.chooseOneClick = chooseOneClick;
            return this;
        }

        public Builder setChooseTwoClick(View.OnClickListener chooseTwoClick) {
            this.chooseTwoClick = chooseTwoClick;
            return this;
        }

        public Builder setChooseThreeClick(View.OnClickListener chooseThreeClick) {
            this.chooseThreeClick = chooseThreeClick;
            return this;
        }

        public Builder setChooseFourClick(View.OnClickListener chooseFourClick) {
            this.chooseFourClick = chooseFourClick;
            return this;
        }

        public ChooseDialog create() {
            View layout = LayoutInflater.from(mContext).inflate(R.layout.choose_dialog_layout, null);
            ChooseDialog chooseDialog = new ChooseDialog(mContext, R.style.PcHeadDialog);
            chooseDialog.setContentView(layout);
            initView(layout);
            initData(chooseDialog);
            setBackgroudClick(layout, chooseDialog);
            return chooseDialog;
        }

        private static void setBackgroudClick(View layout, final ChooseDialog confirmDialog) {
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    confirmDialog.dismiss();
                }
            });
        }

        private void initView(View layout) {
            tvChooseOne = (TextView) layout.findViewById(R.id.tv_dialog_choose_one);
            tvChooseTwo = (TextView) layout.findViewById(R.id.tv_dialog_choose_two);
            tvChooseThree = (TextView) layout.findViewById(R.id.tv_dialog_choose_three);
            tvChooseFour = (TextView) layout.findViewById(R.id.tv_dialog_choose_four);
            viewChooseTwo = layout.findViewById(R.id.view_dialog_choose_two);
            viewChooseThree = layout.findViewById(R.id.view_dialog_choose_three);
            viewChooseFour = layout.findViewById(R.id.view_dialog_choose_four);
        }

        private void initData(final ChooseDialog chooseDialog) {
            setChooseOneData(chooseDialog);
            setChooseTwoData(chooseDialog);
            setChooseThreeData(chooseDialog);
            setChooseFourData(chooseDialog);
        }

        private void setChooseFourData(final ChooseDialog chooseDialog) {
            if (TextUtils.isEmpty(chooseFourString)) {
                viewChooseFour.setVisibility(View.GONE);
            } else {
                viewChooseFour.setVisibility(View.VISIBLE);
                tvChooseFour.setText(chooseFourString);
                viewChooseFour.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != chooseFourClick)
                            chooseFourClick.onClick(v);
                        chooseDialog.dismiss();
                    }
                });
            }
        }

        private void setChooseThreeData(final ChooseDialog chooseDialog) {
            if (TextUtils.isEmpty(chooseThreeString)) {
                viewChooseThree.setVisibility(View.GONE);
            } else {
                viewChooseThree.setVisibility(View.VISIBLE);
                tvChooseThree.setText(chooseThreeString);
                viewChooseThree.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != chooseThreeClick)
                            chooseThreeClick.onClick(v);
                        chooseDialog.dismiss();
                    }
                });
            }
        }

        private void setChooseTwoData(final ChooseDialog chooseDialog) {
            if (TextUtils.isEmpty(chooseTwoString)) {
                viewChooseTwo.setVisibility(View.GONE);
            } else {
                viewChooseTwo.setVisibility(View.VISIBLE);
                tvChooseTwo.setText(chooseTwoString);
                viewChooseTwo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != chooseTwoClick)
                            chooseTwoClick.onClick(v);
                        chooseDialog.dismiss();
                    }
                });
            }
        }

        private void setChooseOneData(final ChooseDialog chooseDialog) {
            if (TextUtils.isEmpty(chooseOneString)) {
                tvChooseOne.setVisibility(View.GONE);
            } else {
                tvChooseOne.setVisibility(View.VISIBLE);
                tvChooseOne.setText(chooseOneString);
                tvChooseOne.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != chooseOneClick)
                            chooseOneClick.onClick(v);
                        chooseDialog.dismiss();
                    }
                });
            }
        }
    }
}
